<?php



namespace App\Zesow\Cache\Inc;

use App\Constant\CacheConstant;
use App\Services\Base\Services\CacheService;
use App\Services\Business\Services\ArticleService;
use App\Services\Base\Interfaces\CacheServiceInterface;
use App\Services\Business\Interfaces\ArticleServiceInterface;

class DoubtViewIncItem extends IncItem
{
    protected $doubtId;

    protected $inc = 1;
    protected $limit = 10;

    public function __construct($doubtId)
    {
        $this->doubtId = $doubtId;
    }

    public function getKey(): string
    {
        return get_cache_key(CacheConstant::DOUBT_VIEW_INCREMENT['name'], $this->doubtId);
    }

    public function save(): void
    {
        /**
         * @var $cacheService CacheService
         */
        $cacheService = app()->make(CacheServiceInterface::class);
        $val = (int)$cacheService->pull($this->getKey());

        /**
         * @var ArticleService $articleService
         */
        $articleService = app()->make(ArticleServiceInterface::class);
        $articleService->viewNumIncrement($this->doubtId, $val);
    }
}
